package org.geektimes.jpa;

import org.geektimes.projects.user.domain.User;

import javax.persistence.*;

/**
 * @FileName: JPATest
 * @Description: JPA测试类
 * @Author: MaWenHao
 * @Created: 2021/3/8 - 下午11:12
 */
@Deprecated
public class JpaTest {

    public static void main(String[] args) {
        EntityManagerFactory entityManagerFactory =
                Persistence.createEntityManagerFactory("myJpa");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction entityTransaction = entityManager.getTransaction();
        entityTransaction.begin();

        // User user = new User();
        // user.setName("liubei");
        // user.setPassword("123456");
        // user.setEmail("gg@163.com");
        // user.setPhoneNumber("18700000000");
        // user.setId(5L);
        //
        // User user1 = entityManager.find(User.class, 5L);
        // User user2 = entityManager.merge(user);
        // System.out.println(user == user1);
        // System.out.println(user1 == user2);

        String sql = "SELECT * FROM users";
        Query nativeQuery = entityManager.createNativeQuery(sql, User.class);
        nativeQuery.getResultStream()
                .forEach(System.out::println);

        // User user = entityManager.find(User.class, 1L);
        // User user = entityManager.getReference(User.class, 1L);
        // System.out.println("-----------------");
        // System.out.println(user);

        // User user = new User();
        // user.setName("lisi");
        // user.setPassword("123456");
        // user.setEmail("bb@163.com");
        // user.setPhoneNumber("18700000000");
        //
        // entityManager.persist(user);
        // System.out.println(user);

        entityTransaction.commit();
        entityManager.close();
        entityManagerFactory.close();
    }
}
